﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Security.Principal; 
using System.Reflection;
using System.Web.Mvc;
using System.Web;
namespace Wedo.Mvc.Utility
{
    /// <summary>
    /// 当前用户
    /// </summary>
    public class CurrentUser : IIdentity
    {

        protected bool _IsAuthenticated;
        protected string _AuthenticationType;
        public CurrentUser()
        { }

        public CurrentUser(string name)
        {
            Name = name;
        }

        public CurrentUser(string name, string[] roles,bool logoned)
        {
            Name = name;
            Roles = roles;
            _IsAuthenticated = logoned;
        }

        public CurrentUser(string name,string  displayName, string[] roles)
        {
            Name = name;
            Roles = roles;
            _IsAuthenticated = true;
            DisplayName = displayName;
        }
        public string DisplayName { get; protected set; }

        public string AuthenticationType
        {
            get { return  _AuthenticationType??"Windows"; }
        }

        public bool IsAuthenticated
        {
            get { return _IsAuthenticated; }
        }

        public string Name { get; protected set; }

        public string[] Roles { get; protected set; }

    }

    public static class SecurityExtensions
    {
        public static string Name(this IPrincipal user)
        {
            return user.Identity.Name;
        }

        public static bool InAnyRole(this IPrincipal user, params string[] roles)
        {
            foreach (string role in roles)
            {
                if (user.IsInRole(role)) return true;
            }
            return false;
        }

        public static CurrentUser GetCurrentUserInfo(this IPrincipal principal)
        {
            if (principal.Identity is CurrentUser)
            {
                return (CurrentUser)principal.Identity;
            }
            else
                return null;
        }
    }

    public static class CookieSecurity
    {
        private static readonly string prefix = "CS_";
        public static CurrentUser GetUser(this HttpRequest Request)
        {
            CurrentUser user = null;
            if (Request.Cookies[prefix + "Name"] != null && Request.Cookies[prefix + "Name"].Value.IsNotNullOrWhiteSpace())
            {
                var roles = Request.GetCValue(prefix + "Roles").Split('|');
                user = new CurrentUser(Request.GetCValue(prefix + "Name"), Request.GetCValue(prefix + "DisplayName"), roles);
            }
            return user;
        }

        public static void CreateUserCookie(this HttpResponse Response, HttpRequest Request, CurrentUser user)
        {
            if (user != null)
            {
                if (IsLogoned(Request, user) == false)
                {
                    Response.CCookie(prefix + "DisplayName", user.DisplayName);
                    Response.CCookie(prefix + "Roles", string.Join("|", user.Roles ?? new string[] { }));
                    Response.CCookie(prefix + "Name", user.Name);
                }
            }
        }

        public static bool IsLogoned(this HttpRequest Request, CurrentUser user)
        {
            if (user == null) return false;

            if (Request.Cookies[prefix + "Name"] != null)
            {
                string userName = Request.Cookies[prefix + "Name"].Value;
                return userName.IsNotNullOrWhiteSpace() && userName != user.Name;
            }
            return false;
        }
    }

}
